<template>
  <div>
    <BasicTable @register="registerTable" :rowSelection="rowSelection">
      <template #tableTitle>
        <a-button preIcon="ant-design:plus-outlined" type="primary" @click="handleAdd">新增</a-button>
        <a-button preIcon="ant-design:plus-outlined" type="primary" @click="handleOpenConfigGroupList">配置组</a-button>
        <a-button type="primary" preIcon="ant-design:export-outlined" @click="onExportXls"> 导出</a-button>
        <j-upload-button type="primary" preIcon="ant-design:import-outlined" @click="onImportXls">导入</j-upload-button>
        <a-dropdown v-if="selectedRowKeys.length > 0">
          <template #overlay>
            <a-menu>
              <a-menu-item key="1" @click="batchHandleDelete">
                <Icon icon="ant-design:delete-outlined"></Icon>
                删除
              </a-menu-item>
            </a-menu>
          </template>
          <a-button>批量操作
            <Icon style="fontSize:12px" icon="ant-design:down-outlined"></Icon>
          </a-button>
        </a-dropdown>
      </template>
      <template #action="{ record }">
        <TableAction :actions="getActions(record)" :dropDownActions="getDropDownAction(record)" />
      </template>
    </BasicTable>
    <ConfigDrawer @register="registerConfigDrawer" @success="reload" />
    <DetailDrawer @register="register" />
    <MyDataGroupList @register="registerGroupDrawer" @success="reload" :bizType="DataGroupBizType.CONFIG"/>
  </div>
</template>
<script lang="ts" name="config" setup>
import { ref } from 'vue';
import { BasicTable, TableAction } from '/@/components/Table';
import { useDrawer } from '/@/components/Drawer';
import ConfigDrawer from './ConfigDrawer.vue';
import DetailDrawer from './DetailDrawer.vue';
import { useGlobSetting } from '/@/hooks/setting';
import { columns, searchFormSchema } from './config.data';
import { 
  listConfig, deleteConfig, batchDeleteConfig, getExportUrl, getImportUrl,
  saveOrUpdateGroup, deleteGroup, batchDeleteGroup
} from './config.api';
import { useListPage } from '/@/hooks/system/useListPage';
import { useMessage } from '/@/hooks/web/useMessage';
import { useRouter } from "vue-router"
import { MyDataGroupList, DataGroupBizType } from "/@/components/BizDataGroup/index"
import { Icon } from '/@/components/Icon';

const router = useRouter()
const { createMessage } = useMessage();
const glob = useGlobSetting();
const [register, { openDrawer: openDetail }] = useDrawer();
const [registerConfigDrawer, { openDrawer: openConfigDrawer }] = useDrawer();
const [registerGroupDrawer, { openDrawer: openGroupDrawer }] = useDrawer();
// 列表页面公共参数、方法
const { prefixCls, onExportXls, onImportXls, tableContext } = useListPage({
  designScope: 'config',
  tableProps: {
    title: '参数配置',
    api: listConfig,
    columns: columns,
    bordered: false,
    formConfig: {
      schemas: searchFormSchema,
      fieldMapToTime: [['fieldTime', ['beginDate', 'endDate'], 'YYYY-MM-DD HH:mm:ss']],
    },
    actionColumn: {
      width: 150,
      title: '操作',
      dataIndex: 'action',
      fixed: 'right',
      slots: { customRender: 'action' },
    },
  },
  exportConfig: {
    name: "参数配置列表",
    url: getExportUrl,
  },
  importConfig: {
    url: getImportUrl
  }
})

const [registerTable, { reload }, { rowSelection, selectedRowKeys }] = tableContext;

function handleOpenConfigGroupList() {
  openGroupDrawer(true, {
    title: '配置组',
    width: 800,
    deleteApi: deleteGroup,
    batchDeleteApi: batchDeleteGroup,
    saveOrUpdateApi: saveOrUpdateGroup
  })
}

/**
 * 新增事件
 */
function handleAdd() {
  openConfigDrawer(true, {
    isUpdate: false,
  });
}

/**
 * 删除事件
 */
async function handleDelete(record) {
  await deleteConfig({ id: record.id }, reload);
}

/**
 * 批量删除事件
 */
async function batchHandleDelete() {
  await batchDeleteConfig({ ids: selectedRowKeys.value }, reload);
}

const handleEditConfig = (record) => {
  openConfigDrawer(true, {
    record,
    isUpdate: true,
  });
}

/**
 * 查看
 */
function handleDetail(record) {
  openDetail(true, {
    record,
    isUpdate: true,
  });
}
/**
 * 操作列定义
 * @param record
 */
function getActions(record) {
  return [
    {
      label: '删除',
      color: 'error',
      ifShow: record.sendStatus != 1,
      popConfirm: {
        title: '是否确认删除',
        confirm: handleDelete.bind(null, record),
      },
    },
    {
      label: '编辑',
      onClick: handleEditConfig.bind(null, record),
      // ifShow: record.sendStatus == 0
    },
    {
      label: '查看',
      onClick: handleDetail.bind(null, record),
    }
  ]
}
/**
 * 下拉操作栏
 */
function getDropDownAction(record) {
  return [
    
  ]
}
</script>
